package Model;

import java.io.Serializable;

import Messaging.Notification;
import Messaging.NotificationManager;

/*
 * @author Roch San Buenaventura
 */

public class PropertySpace extends Space implements Serializable 
{
	final static long serialVersionUID = 217;
	
	protected SpaceState State_;
	protected int rentValues_[];
	
	protected int purchasePrice_, mortgageValue_;
	protected Player Owner_;
	
	protected int Type_, SpaceNumber_;
	
	protected static final String propertyType_[] = {"Purple", "Light Blue", "Brown", "Orange",
		"Red", "Yellow", "Green", "Dark Blue", "Transportation", "Utility"};
	protected static final int propertiesForMonopoly_[] = {2, 3, 3, 3, 3, 3, 3, 2, 4, 2};
	
	public PropertySpace() 
	{
		Name_ = "NONE";
		purchasePrice_ = 0;
		mortgageValue_ = 0;
		State_ = SpaceUnowned.Instance();
		rentValues_ = new int[4];
		Owner_ = null;
	}
	
	public PropertySpace(String name, int type, int spaceNumber, int purchasePrice, int mortgageValue, 
			int rent0, int rent1, int rent2, int rent3) 
	{
		
		Name_ = name;
		purchasePrice_ = purchasePrice;
		mortgageValue_ = mortgageValue;
		State_ = SpaceUnowned.Instance();
		rentValues_ = new int[4];
		rentValues_[0] = rent0;
		rentValues_[1] = rent1;
		rentValues_[2] = rent2; 
		rentValues_[3] = rent3;
		Owner_ = null;
		Type_ = type;
		SpaceNumber_ = spaceNumber;
	}
	
	public PropertySpace(PropertySpace space) 
	{
		Name_ = space.getName();
		purchasePrice_ = space.getPurchasePrice();
		mortgageValue_ = space.getMortgageValue();
		State_ = space.getState();
		rentValues_ = new int[4];
		rentValues_[0] = space.getRentValues()[0];
		rentValues_[1] = space.getRentValues()[1];
		rentValues_[2] = space.getRentValues()[2];
		rentValues_[3] = space.getRentValues()[3];
		
		Owner_ = space.getOwner();
		Type_ = space.getTypeInt();
		SpaceNumber_ = space.getSpaceNumber();
	}
	
	public String toString() 
	{
		String string = "Property Name: " + Name_;
		
		if (Owner_ == null)
			string += ", Owned by: Nobody";
		
		else
			string += ", Owned by: " + Owner_.getName();
		
		return string;
	}
	
	public void changeState(SpaceState newState) 
	{
		State_ = newState;
	}
	
	public void landOn(Player p) 
	{
		State_.landOn(p, this);
	}
	
	public void ownershipIncrease() 
	{
		State_.ownershipIncrease(this);
	}
	
	public void ownershipDecrease() 
	{
		State_.ownershipDecrease(this);
	}

	public String getType() 
	{
		return propertyType_[Type_];
	}
	
	public int getTypeInt() 
	{
		return Type_;
	}
	
	public int getPropertiesForMonopoly(int type) 
	{
		return propertiesForMonopoly_[type];
	}
	
	public int getSpaceNumber() 
	{
		return SpaceNumber_;
	}
	
	public void setOwner(Player p) 
	{
		Owner_ = p;
		NotificationManager.getInstance().notifyObservers(Notification.CHANGED_OWNER, this);
	}
	
	public Player getOwner()
	{
		return Owner_;
	}
	
	public void setPurchasePrice(int purchasePrice) 
	{
		purchasePrice_ = purchasePrice;
	}

	public int getPurchasePrice() 
	{
		return purchasePrice_;
	}

	public void setMortgageValue(int mortgageValue) 
	{
		mortgageValue_ = mortgageValue;
	}

	public int getMortgageValue() 
	{
		return mortgageValue_;
	}
	
	public boolean isUpgradeable() 
	{
		return false;
	}
	
	public boolean isDowngradeable() 
	{
		return false;
	}

	public boolean isRenovated() 
	{
		return false;
	}

	public void bePurchased(Player owner) 
	{
		setOwner(owner);
		SpaceState newState = owner.updateTypeIncrease(Type_);
		
		if (newState != null)
			State_ = newState;
		
		else
			State_ = PropertyOwned.Instance();
	}
	
	public void beMortgaged() 
	{
		State_.changeState(this, SpaceMortgaged.Instance());
		Owner_.updateTypeDecrease(Type_);
	}
	
	public void unmortgage() 
	{
		SpaceState newState = Owner_.updateTypeIncrease(Type_);

		if (newState != null)
			State_ = newState;
		
		else
			State_ = PropertyOwned.Instance();	
	}
	
	public int[] getRentValues() 
	{
		return rentValues_;
	}
	
	public String getNameAndStatus() 
	{
		return propertyType_[Type_] + ": " + Name_ + State_.getNameAndStatus(); 
	}
	
	public SpaceState getState() {
		return State_;
	}
}
